
# chooseCRANmirror()
# 17
# chooseBioCmirror()
# 5
# if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager")
# if (!requireNamespace("clusterProfiler", quietly = TRUE)) BiocManager::install("clusterProfiler")
# if (!requireNamespace("org.Hs.eg.db", quietly = TRUE)) BiocManager::install("org.Hs.eg.db")
library(tidyverse)
library(BiocManager)
library(org.Hs.eg.db)
library(clusterProfiler)

# 以上服务器上先配置好包环境  就不需要装了直接运行 
list <- read.table("list", sep = "\t")
# 是根据给出的名字配上id 使用的
genelist <- bitr(list$V1, fromType="SYMBOL",
                 toType="ENTREZID", OrgDb='org.Hs.eg.db')

#GO 
## 下载基因集的go富集分析 根据给定的id 集合
go <- enrichGO(gene = genelist$ENTREZID,
               OrgDb = org.Hs.eg.db, 
               ont = "all",
               pAdjustMethod = "BH",
               minGSSize = 1,
               pvalueCutoff =1, 
               qvalueCutoff =1,
               readable = TRUE)

go_res <- go@result
write.table(go_res,file="go_all.txt",sep = "\t",row.names = T,col.names = NA,quote = F)

# showNum <- 10
# pdf(file = 'GoBarplot.pdf')
# barplot(go)
# dev.off
# dotplotimg<- dotplot(go)
# print(dotplotimg)
# dotplotimg
# cnetplotad <-cnetplot(go)
# dev.off

#KEGG
#KEGG富集分析
kegg <- enrichKEGG(gene         = genelist$ENTREZID,
                   organism     = 'hsa',
                   pvalueCutoff = 0.1,
                   qvalueCutoff =0.1)
kegg_res <- kegg@result
write.table(kegg_res,file="kegg_all.txt",sep = "\t",row.names = T,col.names = NA,quote = F)
